<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/view/include/taglib.jsp"%>
<html lang="cn" class="app">
<head>
    <meta charset="utf-8" />
    <title>${dict:getConfigValueByKey('WEB_NAME','管理平台')} | 单位管理</title>
    <meta name="description" content="app, web app, responsive, admin dashboard, admin, flat, flat ui, ui kit, off screen nav" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <link rel="icon" type="image/png" href="icon.png">
    <link rel="apple-touch-icon-precomposed" href="icon.png">
    <link rel="stylesheet" href="${ctxStatic}/css/bootstrap.css" type="text/css" />
    <link rel="stylesheet" href="${ctxStatic}/css/app.css" type="text/css" />
    <link rel="stylesheet" href="${ctxStatic}/js/ztree/css/zTreeStyle.css">
    <!--[if lt IE 9]>
    <script src="${ctxStatic}/js/ie/html5shiv.js"></script>
    <script src="${ctxStatic}/js/ie/respond.min.js"></script>
    <script src="${ctxStatic}/js/ie/excanvas.js"></script>
    <![endif]-->
</head>
<body>
<section class="scrollable wrapper">
    <div class="row">
        <div class="col-lg-12">
            <!-- 引导开始 -->
            <ul class="breadcrumb">
                <li><a href="${ctx}/welcome"><i class="fa fa-home"></i> 主页</a></li>
                <li>系统设置</li>
                <li class="active">单位管理</li>
                <li class="active">单位新增</li>
            </ul>
            <!-- 引导结束 -->
        </div>
    </div>
    <form id="orgInsertForm" class="bs-example form-horizontal" >
        <%--基本信息--%>
        <section class="panel panel-default">
            <header class="panel-heading font-bold">单位新增</header>
            <input type="text" id="parentId" name="parentId" value="" />
            <div class="panel-body">
                <%--<div class="col-lg-2 col-md-2 col-sm-2">--%>
                    <%--<div class="text-center m-b m-t">--%>
                        <%--<a href="#" class="thumb-lg">--%>
                            <%--<img src="${ctxStatic}/images/a0.png" class="img-circle">--%>
                        <%--</a>--%>
                        <%--<div>--%>
                            <%--&lt;%&ndash;<div class="h3 m-t-xs m-b-xs">${user.name}</div>&ndash;%&gt;--%>
                            <%--<div class="h3 m-t-xs m-b-xs"><a href="javascript:alert('敬请期待');" class="btn btn-sm btn-info">更换单位照片</a></div>--%>
                            <%--&lt;%&ndash;<small class="text-muted">&ndash;%&gt;--%>
                            <%--&lt;%&ndash;</small>&ndash;%&gt;--%>
                            <%--&lt;%&ndash;<input type="text" class="form-control" name="photo" placeholder="照片" value="${organization.photo}" required />&ndash;%&gt;--%>
                        <%--</div>--%>
                    <%--</div>--%>
                <%--</div>--%>
                <%--<div class="col-lg-10 col-md-10 col-sm-10">--%>
                    <div class="form-group">
                        <label class="col-lg-2 col-md-2 col-sm-2 control-label">上级单位：</label>
                        <div class="col-lg-4 col-md-4 col-sm-4">
                            <input type="text" class="form-control" id="parentName" minlength="1" value="${organization.name}" readonly>
                        </div>
                        <div class="col-lg-4 col-md-4 col-sm-4 ">
                            <button id="menuBtn" onclick="showMenu()" type="button" class="btn btn-s-md btn-default">选择</button>
                        </div>
                        <%--<label class="col-lg-2 col-md-2 col-sm-2 control-label"><span class="text-danger">*</span>照片：</label>--%>
                        <%--<div class="col-lg-4 col-md-4 col-sm-4">--%>
                            <%--<label class="control-label">敬请期待</label>--%>
                            <%--&lt;%&ndash;<input type="file" class="form-control" name="photo" placeholder="照片" value="" required />&ndash;%&gt;--%>
                        <%--</div>--%>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-2 col-md-2 col-sm-2 control-label"><span class="text-danger">*</span>单位名称：</label>
                        <div class="col-lg-4 col-md-4 col-sm-4">
                            <input type="text"  name="name" class="form-control" placeholder="单位名称（必填）" value="" required>
                        </div>
                        <label class="col-lg-2 col-md-2 col-sm-2 control-label"><span class="text-danger">*</span>简称：</label>
                        <div class="col-lg-4 col-md-4 col-sm-4">
                            <input type="text" class="form-control" name="shortName" placeholder="简称" value="" required />
                        </div>
                    </div>
                    <div class="form-group">

                        <label class="col-lg-2 col-md-2 col-sm-2 control-label">是否可用：</label>
                        <div class="col-lg-4 col-md-4 col-sm-4">
                            <select name="available" class="form-control" >
                                <c:forEach items="${yn}" var="m">
                                    <option value="${m.value}" >${m.label}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-lg-offset-2 col-md-offset-2 col-sm-offset-2 col-lg-10 col-md-10 col-sm-10">
                            <shiro:hasPermission name="sys:organization:edit">
                                <button type="button" class="btn btn-s-md btn-info" onclick="insertOrg()">保存</button>
                            </shiro:hasPermission>
                            <button type="button" class="btn btn-s-md btn-danger" onclick="closeSelfModel()">关闭</button>
                        </div>
                    </div>
                <%--</div>--%>
            </div>
        </section>
    </form>
</section>
<div style="height:170px;"></div>
<div id="menuContent" class="menuContent" style="display:none;position: absolute;z-index: 10000;background-color: #eee;border: 1px solid #ccc;">
    <style>.line{height:auto;}</style><%--这个不能删除--%>
    <ul id="tree" class="ztree" style="margin-top:0;"></ul>
</div>
</body>
<script src="${ctxStatic}/js/jquery.min.js"></script>
<!-- Bootstrap -->
<script src="${ctxStatic}/js/bootstrap.js"></script>
<!-- App -->
<script type="text/javascript" src="${ctxStatic}/js/tools.js?version=${ctxVersion}"></script>
<script type="text/javascript" src="${ctxStatic}/js/page/paging.js"></script>
<script type="text/javascript" src="${ctxStatic}/js/layer/layer.js"></script>
<script type="text/javascript" src="${ctxStatic}/js/tmpl/jquery.tmpl.js"></script>
<script src="${ctxStatic}/js/ztree/js/jquery.ztree.core-3.5.min.js"></script>

<script>
    $(function(){
        // 加载单位树
        loadTree();
    })

    // 加载单位数
    function loadTree(){
        var setting = {
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback : {
                onClick : function(event, treeId, treeNode) {
                    //点击回调方法
                    ztreeOnClickCall && ztreeOnClickCall(treeNode);
                    $("#parentName").val(treeNode.name);
                    hideMenu();
                }
            }
        };
        var zNodes =[
            <c:forEach items="${fnc:getMyOrgList()}" var="o" varStatus="status">
            { id:"${o.id}", pId:"${o.parentId}",no:'${o.no}', name:"${o.name}", open:true}<c:if test="${!status.last}">,</c:if>
            </c:forEach>
        ];
        var ztree = $.fn.zTree.init($("#tree"), setting, zNodes);
        ztree.expandAll(true);
    }



    //ztree点击回调
    function ztreeOnClickCall(treeNode){
        alert(treeNode.id);
        $("#parentId").val(treeNode.id);
    }
    //展示树选择区域
    function showMenu() {
        var cityObj = $("#parentName");
        var cityOffset = $("#parentName").offset();
        $("#menuContent").css({
            left: cityOffset.left + "px",
            top: cityOffset.top + cityObj.outerHeight() + "px"
        }).slideDown("fast");

        $("body").bind("mousedown", onBodyDown);
    }
    //隐藏树选择区域
    function hideMenu() {
        $("#menuContent").fadeOut("fast");
        $("body").unbind("mousedown", onBodyDown);
    }
    //点击其他区域事件-用于隐藏选择树
    function onBodyDown(event) {
        if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length > 0)) {
            hideMenu();
        }
    }

    /**更新单位 @author gaoy*/
    function insertOrg(){
        var loadingLayer = layer.load(1, {shade: [0.1,'#fff']});//loading
        //根据项目标识获取目录
        $.ajax({
            type: 'post',
            url: '${ctx}/organization/insertOrg.json?timeStamp='+new Date().getTime(),
            dataType:"json",
            data: $("#orgInsertForm").serialize(),
            success: function (data) {
                layer.close(loadingLayer);
                if(data.ret == "1"){
                    showMsg(data.desc,function(){
                        location.href = "${ctx}/organization/update?id="+data.data;
                    });
                }else{
                    showMsg(data.desc);
                }
            },error:function(){
                layer.close(loadingLayer);
                showMsg("点击过快或程序异常");
            }
        });
    }

    /** 关闭当前弹出框并执行 */
    function closeSelfModel(){
        var index = parent.layer.getFrameIndex(window.name);
        parent.searchPage(1);
        parent.loadTree();
        parent.layer.close(index);
    }
</script>
</html>
